﻿using System.Linq;
using System.Web.Mvc;
using Project.DAL.Repository;
using Project.Domain.Entities;

namespace Project.WebUI.Areas.Admin.Controllers
{
    public class ManageContentController : Controller
    {

        #region "private"
        private readonly IUserRepository _userRepository;
        private readonly IDepositRepository _depositRepository;
        private readonly IWithDrawRepository _withDrawRepository;
        private readonly IUserService _userService;
        private readonly ICountryRepository _countryRepository;
        private readonly IContactRepository _contactRepository;        
        private readonly IStoreSecureRepositoty _storeSecureRepositoty;
        private readonly IContentRepository _contentRepository;
        private const string Controller = "ManageContent";

        public ManageContentController(
            IUserRepository userRepository,
            IDepositRepository depositRepository,
            IWithDrawRepository withDrawRepository,
            IUserService userService,
            ICountryRepository countryRepository,
            IContactRepository contactRepository,            
            IStoreSecureRepositoty storeSecureRepositoty,
            IContentRepository  contentRepository
            )
        {
            _userRepository = userRepository;
            _depositRepository = depositRepository;
            _withDrawRepository = withDrawRepository;
            _userService = userService;
            _countryRepository = countryRepository;
            _contactRepository = contactRepository;            
            _storeSecureRepositoty = storeSecureRepositoty;
            _contentRepository = contentRepository;
        }

        #endregion                

        #region "LoadAjax"

        public ActionResult Index()
        {
            if (Session["admin"] == null)
            {
                return RedirectToAction("Login","Dashboard");
            }
            return View();
        }

        public ActionResult LoadAjax(int index, int numpage)
        {
            var lisContent = _contentRepository.GetAll();
            ViewBag.result = lisContent.Count();
            return PartialView("_MainContent", lisContent);
        }

        #endregion

        #region "UpdateStatus"
        public ActionResult UpdateStatus(int id, int status)
        {
            var content = _contentRepository.GetById(id);
            if (status == 1)
            {
                content.Active = false;
                _contentRepository.SaveChanges();
                ViewBag.status = false;
                ViewBag.id = id;
                ViewBag.controller = Controller;
                return PartialView("_UpdateStatus");
            }
            content.Active = true;
            _contentRepository.SaveChanges();
            ViewBag.status = true;
            ViewBag.id = id;
            ViewBag.controller = Controller;
            return PartialView("_UpdateStatus");
        }

        #endregion

        #region "UpdateContent"
        public ActionResult UpdateContent(int id)
        {
            var content = _contentRepository.GetById(id);
            return content != null ? View(content) : View();
        }

        [HttpPost]
        [ValidateInput(true)]
        [ValidateAntiForgeryToken]
        public ActionResult UpdateContent(Content contentModel)
        {
            var status=_contentRepository.Update(contentModel);                        
            if(status)
                _contentRepository.SaveChanges();
            return View();
        }

        #endregion

        #region "CreateContent"
        public ActionResult CreateContent()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateContent(Content contentModel)
        {
            _contentRepository.Add(contentModel);
            _contentRepository.SaveChanges();           
            return RedirectToAction("Index");
        }

        #endregion       
    }
}